package org.example.ch10.service;

import org.example.ch10.dto.Message;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @ClassName PublishService
 * @Description TODO
 * @Author Kx
 * @Date 2025/9/21 19:36
 * @Version 1.0
 */
@Service
public class PublishService {
    private SimpMessagingTemplate template;

    public PublishService(SimpMessagingTemplate template) {
        this.template = template;
    }

    /**
     * @author: kx
     * @date: 2025/9/21 19:39
     * @param topic
     * @param message : 消息
     * @description: 消息投递
    */
    public void sendMessage(String topic, String message) {
        Message msg = new Message();
        msg.setContent(message);
        msg.setSendDate(new Date());
        // 将消息发布到 Broker 中，/news 是配置的前缀，/message 是自定义的路径
        template.convertAndSend("/" + topic + "/message", msg);
    }
}
